package com.feiyu.stackqueue;

/**
 * @Author nanshouxiao@teacher.com.cn
 * @Date 2017-10-27 16:45
 */
public class BaseQueue<T> {
    protected Object[] datas;

    protected int capacity;

    protected int start;

    protected int end;

    protected int size;

    public BaseQueue() {
        this(10);
    }

    public BaseQueue(int capacity) {
        start = 0;
        end = 0;
        size = 0;
        this.capacity = capacity;
        datas = new Object[capacity];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == capacity;
    }

    public T push(T t) {
        if (isFull()) {
            System.out.println("队列已满，插入失败");
            return null;
        }
        System.out.println(position());
        datas[end++] = t;
        size++;
        if (end >= capacity) {
            end = end%capacity;
        }
        return t;
    }

    public T pop() {
        if (isEmpty()) {
            return null;
        }
        T result = (T) datas[start++];
        size--;
        if (start >= capacity) {
            start = start % capacity;
        }
        return result;
    }

    public T peek() {
        if (isEmpty()) {
            return null;
        }
        return (T) datas[start];
    }

    public String position() {
        return "size:" + size + " [" + start + " : " + end + "]";
    }
}
